package com.tianma315.admin.check.controller;


import java.util.Arrays;
import java.util.List;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.check.domain.CheckTypeDO;
import com.tianma315.admin.check.service.CheckTypeService;

/**
 * 
 * <pre>
 * 检查类型
 * </pre>
 * <small> 2019-05-25 10:03:07 | Aron</small>
 */
@Controller
@RequestMapping("/check/checkType")
public class CheckTypeController  extends BaseController {
	@Autowired
	private CheckTypeService checkTypeService;
	
	@GetMapping()
	@RequiresPermissions("check:checkType:checkType")
	String CheckType(){
	    return "check/checkType/checkType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("check:checkType:checkType")
	public Result<Page<CheckTypeDO>> list(Integer pageNumber, Integer pageSize, CheckTypeDO checkTypeDTO){
		// 查询列表数据
        Page<CheckTypeDO> page = checkTypeService.getCheckTypePage(pageNumber,pageSize,checkTypeDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("check:checkType:add")
	String add(){
	    return "check/checkType/add";
	}

	@GetMapping("/edit/{checkTypeId}")
	@RequiresPermissions("check:checkType:edit")
	String edit(@PathVariable("checkTypeId") Integer checkTypeId,Model model){
		CheckTypeDO checkType = checkTypeService.selectById(checkTypeId);
		model.addAttribute("checkType", checkType);
	    return "check/checkType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("check:checkType:add")
	public Result<String> save( CheckTypeDO checkType){
		if(checkTypeService.addCheckType(checkType)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("check:checkType:edit")
	public Result<String>  update( CheckTypeDO checkType){
		checkTypeService.updateById(checkType);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("check:checkType:remove")
	public Result<String>  remove( Integer checkTypeId){
		if(checkTypeService.deleteCheckType(checkTypeId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("check:checkType:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] checkTypeIds){
//		checkTypeService.deleteBatchIds(Arrays.asList(checkTypeIds));
		for (int i = 0; i < checkTypeIds.length; i++) {
			checkTypeService.deleteCheckType(checkTypeIds[i]);
		}
		return Result.ok();
	}


	/**
	 * 所有数据
	 */
	@GetMapping( "/getCheckTypeList")
	@ResponseBody
	public Result<List<CheckTypeDO>>  getCheckTypeList( CheckTypeDO checkTypeDO){
		Wrapper<CheckTypeDO> wrapper = new EntityWrapper<CheckTypeDO>();
		wrapper.eq("state",0);
		wrapper.like("type_name",checkTypeDO.getTypeName());
		List<CheckTypeDO> list = checkTypeService.selectList(wrapper);
		return Result.ok(list);
	}
	
}
